import { QueryClient, useMutation } from "@tanstack/react-query";
import { useForm } from "react-hook-form";
import { userControllerSetRole } from "@/shared/api/api.user";
import { AxiosError } from "axios";
import { DisciplineService } from "@/shared/api/api.discipline";
import { ThemeService } from "@/shared/api/api.theme";

export function useCreateTheme(disciplineId:number, level:number) {
    const { register, handleSubmit } = useForm<{
        tittle: string;
        award: number;
    }>();
    const queryClient = new QueryClient

    const createThemeMutation = useMutation(
        {
            mutationFn: ThemeService.create,
            onError: (error: (AxiosError<{ message: string }>)) => error,
        }, queryClient
    );
    const errorMessage = createThemeMutation.error ? createThemeMutation.error.response?.data.message : undefined;

    return {
        register,
        errorMessage,
        isSuccess: createThemeMutation.isSuccess,
        handleSubmit: handleSubmit((data) => createThemeMutation.mutate({ ...data, award: Number(data.award), disciplineId: Number(disciplineId), level:level })),
    };
}